package com.elong.android.specialhouse.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.alibaba.fastjson.JSONArray;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.RegionResult;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @ClassName: FileUtils
 * @Description: SharedPreferences操作工具类
 * @author kaiqi.li
 * @date 2015-8-5 下午3:29:37
 */
public class PrefUtils {

	private static final String TAG = PrefUtils.class.getSimpleName() ;

	public static final String PREF_SP_FILE_NAME_HTTPHEADER = "HttpHeader";
	public static final String PREF_SP_FILE_NAME_PUSH = "PushSetting";
	public static final String PREF_CHANNEL_ID = "channelId" ;
	public static final String SEARCH_HISTORIES = "searchHistories" ;
	public static final String 	PREF_IF_HAVE_NEW_MESSAGE  = "ifHaveNewMessage" ;
	public static final String 	PREF_IS_OFF = "isoff" ;
	public static final String 	PREF_IS_BINDED_PUSH_SERVICE = "isBinded" ;
	public static final String 	PREF_CID = "cid" ;
	public static final String VERSION_CODE = "versionCode";
	public static final String 	PHONE_NUM = "phone" ;


	/**
	 * 设置是否有新消息标志
	 * @param context
	 * @param isHaveNewMessage
	 */
	public static void setIfHaveNewMessage(Context context, boolean isHaveNewMessage) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(PREF_IF_HAVE_NEW_MESSAGE,isHaveNewMessage);
		editor.commit() ;
	}

	/**
	 * 获取是否有新消息的标志
	 * @param context
	 * @return
	 */
	public static boolean getIfHaveNewMessage(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		return sp.getBoolean(PREF_IF_HAVE_NEW_MESSAGE,false);
	}

	/**
	 * 保存推送开关状态
	 * @param context
	 * @param isReceive
	 */
	public static void setIsReceivePush(Context context ,boolean isReceive) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(PREF_IS_OFF,isReceive);
		editor.commit() ;
	}

	/**
	 * 获取推送开关状态，默认为开
	 * @param context
	 * @return
	 */
	public static boolean getIsRecievePush(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		return sp.getBoolean(PREF_IS_OFF, true);
	}

	/**
	 * 设置是否已经绑定uid和cid的标志位
	 * @param context
	 * @param isBind
	 */
	public static void setIsBindingPushService(Context context, boolean isBind) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(PREF_IS_BINDED_PUSH_SERVICE, isBind);
		editor.commit() ;
	}

	/**
	 * 获取是否已经绑定uid和cid的标志位
	 * @param context
	 * @return
	 */
	public static boolean getIsBindingPushService(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, Context.MODE_PRIVATE);
		return sp.getBoolean(PREF_IS_BINDED_PUSH_SERVICE, false);
	}

	public static void setClientId(Context context,String cid) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_HTTPHEADER, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(PREF_CID, cid);
		editor.commit();
	}

	public static String getClientId(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_HTTPHEADER, Context.MODE_PRIVATE);
		return sp.getString(PREF_CID, "");
	}
	/**
	 * 清除指定的SharedPreferences文件
	 * @param context
	 */
	public static void clear(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_PUSH, context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.clear() ;
		editor.commit() ;
	}
	/**
	 * 
	 * @Title: saveFirstInstalledChannelID
	 * @Description: 保存第一次安装的渠道号
	 * @param @return
	 * @return String
	 */
	public static void saveFirstInstalledChannelID(String channelId , Context context) {
		SharedPreferences channelData = context.getSharedPreferences(PREF_SP_FILE_NAME_HTTPHEADER, Context.MODE_PRIVATE);
		Editor editor = channelData.edit();
		editor.putString(PREF_CHANNEL_ID, channelId);
		editor.commit();
	}
	
	
	/**
	 * 
	 * @Title: saveFirstInstalledChannelID
	 * @Description: 保存第一次安装的渠道号
	 * @param @return
	 * @return String
	 */
	public static String getChannelId(Context context){
		SharedPreferences sp = context.getSharedPreferences(PREF_SP_FILE_NAME_HTTPHEADER, Context.MODE_PRIVATE);
		return sp.getString(PREF_CHANNEL_ID, "");
	}

	/**
	 * 获取历史搜索记录
	 * @param context
	 * @return JSONString
	 */
	public static String getSearchHistories(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		return sp.getString(SEARCH_HISTORIES,"");
	}

	/**
	 * 保存历史搜索记录
	 * @param context
	 * @param regionResult
	 * @return JSONString
	 */
	public static void saveSearchHistories(Context context , RegionResult regionResult) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = sp.edit();

		String histories = getSearchHistories(context);
		List<RegionResult> resions = new ArrayList<RegionResult>();
		if (!"".equals(histories)) {
			resions = JSONArray.parseArray(histories, RegionResult.class);
			if (resions.contains(regionResult)) {
				return;
			}
			if (resions.size() < 3) {
				resions.add(0, regionResult);
				editor.putString(SEARCH_HISTORIES, JSONArray.toJSONString(resions));
				editor.commit();
			} else {
				resions.remove(resions.size() - 1);
				resions.add(0, regionResult);
				editor.putString(SEARCH_HISTORIES, JSONArray.toJSONString(resions));
				editor.commit();
			}
		} else {
			resions.add(regionResult) ;
			editor.putString(SEARCH_HISTORIES , JSONArray.toJSONString(resions));
			editor.commit() ;
		}

	}

	/**
	 * 保存App版本号信息
	 * @param context
	 * @param versionCode
	 */
	public static void putVersionCode(Context context, int versionCode) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = sp.edit() ;
		editor.putInt(VERSION_CODE, versionCode);
		editor.commit() ;
	}

	/**
	 * 获取已保存的App版本号
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		return sp.getInt(VERSION_CODE, -1);
	}
	/**
	 * 保存联系我们中电话号码
	 * @param context
	 * @param phoneNum
	 */
	public static void setPhoneNum(Context context ,String phoneNum) {
		if(!StringUtils.isEmpty(phoneNum)){
			SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
			Editor editor = sp.edit();
			editor.putString(PHONE_NUM, phoneNum);
			editor.commit() ;
		}
	}

	/**
	 * 获取联系我们中的电话号码
	 * @param context
	 * @return
	 */
	public static String getPhoneNum(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		return sp.getString(PHONE_NUM, context.getString(R.string.contact_us_phone));
	}
}
